package com.eternal.control.view;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;

import androidx.core.content.ContextCompat;
import androidx.core.view.MotionEventCompat;

import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.eternal.base.utils.GuQiangUtil;
import com.eternal.control.R;
import com.eternal.framework.utils.ConvertUtils;

import java.util.Locale;

import static java.lang.Math.PI;

public class GuQiangCycleDialView extends View {
    private static final String FAH = "℉";
    private static final String DEGREE = "℃";
    private Bitmap background;
    private int mWidth;//控件宽
    private int maxR;//最大半径 真个圆圈的最大可显示半径
    private int outCycleRadius = 20;//外圆外边距
    private int mMiddRStrok = 40;//中间圆的宽度
    private int mMiddRadias;//中间圆的半径
    private int outSidePadding = 26;//最外圆内边距
    private int mInnerCycleWithMidCycleMargin = 11;//内圆与中圆的间距
    private int slidingBlockRadias = 13;//滑点的半径
    private Paint mInnerPaint;//内部圆
    private Paint mArcCirclePaint;//圆弧上的圆
    private Paint mArcTextPaint;//圆弧的文字画笔
    private Paint mStatusTextPaint;//状态线画笔
    private int mInnerR = 67;//内部圆的半径
    private final static int INNER_ON_OR_OFF = 1;//on或者off模式 画中心的数据
    private final static int INNER_AUTO = 2;//AUTO模式 画中心的数据
    private final static int INNER_TIME_TO_ON_OR_OFF = 3;//TimeToOn/Off模式 画中心的数据
    private final static int INNER_CYCLE = 4;//Cycle模式 画中心的数据
    private final static int INNER_SCHEDULE = 5;//schedule模式 画中心的数据

    public final static int MODE_TYPE_ON = 1;
    public final static int MODE_TYPE_OFF = 2;
    public final static int MODE_TYPE_CYCLE = 3;
    public final static int MODE_TYPE_AUTO_TMP_C = 4;//Auto-----温度模式
    public final static int MODE_TYPE_AUTO_TMP_F = 5;//Auto-----华氏度模式
    public final static int MODE_TYPE_AUTO_HUM = 6;//Auto-----湿度模式
    public final static int MODE_TYPE_SCHEDULE = 7;//SCHEDULE模式
    public final static int MODE_TYPE_TIME_TO_ON = 8;//TIME_TO_ON模式
    public final static int MODE_TYPE_TIME_TO_OFF = 9;//TIME_TO_OFF模式
    private int mCurrentMode = MODE_TYPE_AUTO_TMP_F;//当前圆盘模式
    private int maxTotal = 50;//最大刻度可显示数量
    private int realMaxTotal = 50;//实际滑动过程中的刻度读数数量
    private float tempTextAngle;//滑动数值计算的间隔
    private float tempAngle;//指针角度间隔
    private Paint mArcPaint;//圆弧画笔
    private int mArcKeduLenght = 15;//圆弧的刻度长度

    private boolean isStartDraging;//是否开始滑动
    private boolean isStartCanDraging;//开始目标刻度是否可以滑动
    private boolean isEndCanDraging;//结束目标刻度是否可以滑动

    private float needRotatteSartAngel;//开始目标需要旋转的角度
    private float needRotatteEndAngel;//结束目标需要旋转的角度

    private float needRoatteStatusAngel;//状态线角度
    private String statusCenterText = "";//中间状态文字内容
    private String statusCenterBottomText = "";//中间状态下部文字内容
    private String innerCircleTime = "";//内部圆的时间
    private String innerCircleTmp = "";//内部圆的温度
    private String innerCircleTmpUnit = "";//内部圆的温度单位
    private String innerCircleSpeed = "";//内部圆的风速
    private byte innerState = 0;
    private boolean isDeviceC;


    private int currentStartTag = 0;//当前开始滑动角度的刻度
    private int currentEndTag = 0;//当前结束滑动角度的刻度

    private float mStartTagViewRealRotateAngel;//开始小球实际滑动的角度（仅作为判断依据--四舍五入的关系needRotatteStartAngel经过了换算）
    private float mEndTagViewRealRotateAngel;//结束小球实际滑动的角度（仅作为判断依据--四舍五入的关系needRotatteEndAngel经过了换算）

    private int currentEndShowText;//当前显示在界面上的结束刻度
    private boolean isMoved;//是否滑动了 防止点击出现滑动

    private int currentStartShowText;//当前显示在界面上的开始刻度
    private String mCurrentCenterBottomShowText;//当前中圆底部显示数据

    private Paint midSideBottomTextPaint;

    private OnChangeListeners mOnChangeListeners;//滑动值的监听
    private ObjectAnimator mProgressAnim;
    private int mAlpha = 255;//透明度
    private boolean showOffOrOn, startClose, endClose;
    private int showFan;
    private float innerTimeTextSize, innerTmpHumTextSize;

    public GuQiangCycleDialView(Context context) {
        super(context);
        initView();
    }

    public GuQiangCycleDialView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public GuQiangCycleDialView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int specSize = MeasureSpec.getSize(widthMeasureSpec);
        // 参考宽，处理成正方形
        setMeasuredDimension(specSize, specSize);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        // 控件宽、高
        mWidth = Math.min(h, w);
        // 温度，刻度的半径
        maxR = mWidth / 2;
        mInnerR = (int) (0.4 * maxR);
        mInnerCycleWithMidCycleMargin = (int) (0.08 * maxR);
        mMiddRStrok = (int) (maxR * 0.28);
        outSidePadding = (int) (maxR * 0.16);
        slidingBlockRadias = (int) (maxR * 0.08);
        innerTimeTextSize = (mInnerR * 3 / 2f) / mInnerPaint.measureText("8:33") * mInnerPaint.getTextSize();
        innerTmpHumTextSize = (mInnerR * 3 / 2f) / mInnerPaint.measureText("188") * mInnerPaint.getTextSize();
        outCycleRadius = mInnerR + mInnerCycleWithMidCycleMargin + mMiddRStrok + outSidePadding;
        if (mWidth != 0) {
            initBackground();
        }
    }

    public void setDeviceC(boolean b) {
        isDeviceC = b;
    }

    /**
     * 初始化相关画笔
     */
    private void initView() {
//        //初始化内圆画笔
        initInnerCyclePaint();
        //初始化圆弧画笔
        initArcPaint();
        //初始化中圆底部画笔
        initCenterCycleBottomTextPaint();
        //初始化状态线
        initStatusTextPaint();
        //初始化动画
        initAnimator();
    }

    public void showFan(int showFan) {
        this.showFan = showFan;
    }

    public void showOffOrOn(boolean b) {
        this.showOffOrOn = b;
    }

    /**
     * 初始化状态线
     */
    private void initStatusTextPaint() {
        mStatusTextPaint = new Paint();
        mStatusTextPaint.setAntiAlias(true);
        mStatusTextPaint.setStyle(Paint.Style.FILL);
    }

    /**
     * 初始化中圆底部画笔
     */
    private void initCenterCycleBottomTextPaint() {
        midSideBottomTextPaint = new Paint();
        midSideBottomTextPaint.setAntiAlias(true);
        midSideBottomTextPaint.setStyle(Paint.Style.FILL);
    }


    /**
     * 初始化圆弧画笔
     */
    private void initArcPaint() {
        mArcPaint = new Paint();
        mArcPaint.setAntiAlias(true);
        mArcCirclePaint = new Paint();
        mArcCirclePaint.setAntiAlias(true);
        mArcCirclePaint.setStyle(Paint.Style.FILL);
        mArcCirclePaint.setStrokeWidth(ConvertUtils.dp2px(20));
        mArcCirclePaint.setColor(ContextCompat.getColor(getContext(), android.R.color.white));
        mArcTextPaint = new Paint();
        mArcTextPaint.setAntiAlias(true);
    }


    /**
     * 开始画图
     *
     * @param canvas 画布
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (background != null) {
            canvas.drawBitmap(background, 0, 0, mInnerPaint);
        }
        //画内圆的文字
        drawInnerText(canvas);
        //画目标可移动的圆球
        drawStartTagView(canvas);
        //画圆弧
        if (showFan != 0 || (mCurrentMode != MODE_TYPE_OFF && mCurrentMode != MODE_TYPE_ON)) {
            drawArc(canvas);
        }
//        画中圆底部
        drawMidCircleBottomText(canvas);
        //画状态线
        if (showFan != 0 || (mCurrentMode != MODE_TYPE_OFF && mCurrentMode != MODE_TYPE_ON)) {
            drawStatusLine(canvas);
        }
    }

    /**
     * 画状态线
     *
     * @param canvas
     */
    private void drawStatusLine(Canvas canvas) {
        LinearGradient mLinearGradient = new LinearGradient(0, -(mMiddRadias + mMiddRStrok / 2f) + ConvertUtils.dp2px(14), 0, -ConvertUtils.dp2px(mInnerR), new int[]{getResources().getColor(R.color.color_FF4709), getResources().getColor(R.color.color_FF8D00)}, null, Shader.TileMode.CLAMP);
        mStatusTextPaint.setShader(mLinearGradient);
        mStatusTextPaint.setStrokeWidth(ConvertUtils.dp2px(2f));
        canvas.save();
        canvas.translate(maxR, maxR);
        canvas.rotate(-150f);
        //根据模式画状态线
        if ((mCurrentMode == MODE_TYPE_TIME_TO_ON || mCurrentMode == MODE_TYPE_TIME_TO_OFF) && isStartCanDraging) {
            canvas.rotate(needRotatteSartAngel);
            needRoatteStatusAngel = needRotatteSartAngel;
        } else if (mCurrentMode == MODE_TYPE_CYCLE) {
            if (isEndCanDraging) {
                canvas.rotate(158 + needRotatteEndAngel);
                needRoatteStatusAngel = 158 + needRotatteEndAngel;
            } else if (isStartCanDraging) {
                canvas.rotate(needRotatteSartAngel);
                needRoatteStatusAngel = needRotatteSartAngel;
            } else {
                canvas.rotate(needRoatteStatusAngel);
            }
        } else if (mCurrentMode == MODE_TYPE_OFF || mCurrentMode == MODE_TYPE_ON) {
            canvas.rotate(needRotatteSartAngel);
        } else {
            canvas.rotate(needRoatteStatusAngel);
        }
        canvas.drawLine(0, -(mMiddRadias + (int) (0.625 * slidingBlockRadias)), 0, -mInnerR, mStatusTextPaint);
        //恢复画布坐标
        canvas.restore();
    }

    /**
     * 画中圆底部文字
     *
     * @param canvas
     */
    private void drawMidCircleBottomText(Canvas canvas) {
        switch (mCurrentMode) {
            case MODE_TYPE_ON:
                drawMidCircleBottomTextByOffOrOn(canvas, "ON");
                break;
            case MODE_TYPE_OFF:
                drawMidCircleBottomTextByOffOrOn(canvas, "OFF");
                break;
            case MODE_TYPE_AUTO_TMP_C:
            case MODE_TYPE_AUTO_TMP_F:
            case MODE_TYPE_AUTO_HUM:
                if (isDeviceC) {
                    drawMidCircleBottomTextByAuto(canvas, "ON");
                } else {
                    drawMidCircleBottomTextByAuto(canvas, "AUTO");
                }
                break;
            case MODE_TYPE_TIME_TO_ON:
                drawMidCircleBottomTextByTimeToOnOff(canvas, "TIMER ON");
                break;
            case MODE_TYPE_TIME_TO_OFF:
                drawMidCircleBottomTextByTimeToOnOff(canvas, "TIMER OFF");
                break;
            case MODE_TYPE_CYCLE:
                drawMidCircleBottomTextByCycle(canvas, "CYCLE");
                break;
            case MODE_TYPE_SCHEDULE:
                drawMidCircleBottomTextBySchedule(canvas, "SCHEDULE");
                break;
        }
    }

    /**
     * ON 和 OFF 模式
     * 画中圆底部速度和模式
     *
     * @param canvas
     * @param modeType
     */
    private void drawMidCircleBottomTextByOffOrOn(Canvas canvas, String modeType) {
        midSideBottomTextPaint.setTextSize(slidingBlockRadias * 1.2f);
        midSideBottomTextPaint.setColor(getResources().getColor(R.color.color_FFFFFF));
        midSideBottomTextPaint.setTextAlign(Paint.Align.CENTER);
        int modeTypeHeight = caculateTextCenterHeightDisTance(midSideBottomTextPaint);
        float y = maxR + mInnerR + mInnerCycleWithMidCycleMargin +
                (mMiddRStrok - modeTypeHeight) / 2f;
        if (isStartCanDraging) {
            midSideBottomTextPaint.setTextAlign(Paint.Align.RIGHT);
            canvas.drawText("Set", maxR - ConvertUtils.dp2px(2), y + modeTypeHeight, midSideBottomTextPaint);
            //画中心风速和上下图标
            Bitmap fanNormal = BitmapFactory.decodeResource(getResources(), showFan == 2 ? R.mipmap.control_cicle_small : R.mipmap.kongzhi_fengshan_icon);
            int fanNormalWidth = fanNormal.getWidth();
            int fanNormalHeight = fanNormal.getHeight();
            float fanLeft = maxR + ConvertUtils.dp2px(3);
            canvas.drawBitmap(fanNormal, null, new RectF(fanLeft, y, fanLeft + fanNormalWidth, y + fanNormalHeight), midSideBottomTextPaint);
            midSideBottomTextPaint.setTextAlign(Paint.Align.LEFT);
            canvas.drawText(mCurrentCenterBottomShowText, maxR + ConvertUtils.dp2px(5) + fanNormalWidth,
                    y + modeTypeHeight, midSideBottomTextPaint);
        } else {
            canvas.drawText(modeType, maxR, y + modeTypeHeight, midSideBottomTextPaint);
        }
    }

    /**
     * TIMETOON 和 TIMETOOFF 模式
     * 画中圆底部时间和模式
     *
     * @param canvas
     * @param modeType
     */
    private void drawMidCircleBottomTextByTimeToOnOff(Canvas canvas, String modeType) {
        midSideBottomTextPaint.setTextSize(slidingBlockRadias * 1.2f);
        midSideBottomTextPaint.setColor(getResources().getColor(R.color.color_FFFFFF));
        midSideBottomTextPaint.setTextAlign(Paint.Align.CENTER);
        int modeTypeHeight = caculateTextCenterHeightDisTance(midSideBottomTextPaint);
        float y = maxR + mInnerR + mInnerCycleWithMidCycleMargin +
                mMiddRStrok / 2f;
        if (isStartCanDraging) {
            canvas.drawText("Set", maxR, y, midSideBottomTextPaint);
            canvas.drawText(mCurrentCenterBottomShowText, maxR, y + ConvertUtils.dp2px(4) + modeTypeHeight, midSideBottomTextPaint);
        } else {
            canvas.drawText(modeType, maxR, y + modeTypeHeight / 2f, midSideBottomTextPaint);
        }
    }

    /**
     * Cycle 模式
     * 画中圆底部时间和模式
     *
     * @param canvas
     * @param modeType
     */
    private void drawMidCircleBottomTextByCycle(Canvas canvas, String modeType) {
        midSideBottomTextPaint.setTextSize(slidingBlockRadias * 1.2f);
        midSideBottomTextPaint.setColor(getResources().getColor(R.color.color_FFFFFF));
        midSideBottomTextPaint.setTextAlign(Paint.Align.CENTER);
        int modeTypeHeight = caculateTextCenterHeightDisTance(midSideBottomTextPaint);
        float y = maxR + mInnerR + mInnerCycleWithMidCycleMargin +
                mMiddRStrok / 2f;
        if (isStartCanDraging) {
            canvas.drawText("Set On", maxR, y, midSideBottomTextPaint);
            canvas.drawText(mCurrentCenterBottomShowText, maxR, y + ConvertUtils.dp2px(4) + modeTypeHeight, midSideBottomTextPaint);
        } else if (isEndCanDraging) {
            canvas.drawText("Set Off", maxR, y, midSideBottomTextPaint);
            canvas.drawText(mCurrentCenterBottomShowText, maxR, y + ConvertUtils.dp2px(4) + modeTypeHeight, midSideBottomTextPaint);
        } else {
            canvas.drawText(modeType, maxR, y + modeTypeHeight / 2f, midSideBottomTextPaint);
        }
    }

    /**
     * SCHEDULE 模式
     * 画中圆底部时间和模式
     *
     * @param canvas
     * @param modeType
     */
    private void drawMidCircleBottomTextBySchedule(Canvas canvas, String modeType) {
        midSideBottomTextPaint.setTextSize(slidingBlockRadias * 1.2f);
        midSideBottomTextPaint.setColor(getResources().getColor(R.color.color_FFFFFF));
        midSideBottomTextPaint.setTextAlign(Paint.Align.CENTER);
        int modeTypeHeight = caculateTextCenterHeightDisTance(midSideBottomTextPaint);
        float y = maxR + mInnerR + mInnerCycleWithMidCycleMargin +
                mMiddRStrok / 2f;
        if (isStartCanDraging) {
            canvas.drawText("Set Start", maxR, y, midSideBottomTextPaint);
            canvas.drawText(mCurrentCenterBottomShowText, maxR, y + ConvertUtils.dp2px(2) + modeTypeHeight, midSideBottomTextPaint);
        } else if (isEndCanDraging) {
            canvas.drawText("Set End", maxR, y, midSideBottomTextPaint);
            canvas.drawText(mCurrentCenterBottomShowText, maxR, y + ConvertUtils.dp2px(2) + modeTypeHeight, midSideBottomTextPaint);
        } else {
            canvas.drawText(modeType, maxR, y + modeTypeHeight / 2f, midSideBottomTextPaint);
        }
    }

    public void setStartCanDraging(boolean b) {
        isStartCanDraging = b;
    }

    public void setEndCanDraging(boolean b) {
        isEndCanDraging = b;
    }

    /**
     * AUTO 模式
     * 画中圆底部时间和模式
     *
     * @param canvas
     * @param modeType
     */
    private void drawMidCircleBottomTextByAuto(Canvas canvas, String modeType) {
        midSideBottomTextPaint.setTextSize(slidingBlockRadias * 1.2f);
        midSideBottomTextPaint.setColor(getResources().getColor(R.color.color_FFFFFF));
        midSideBottomTextPaint.setTextAlign(Paint.Align.CENTER);
        int modeTypeHeight = caculateTextCenterHeightDisTance(midSideBottomTextPaint);
        float y = maxR + mInnerR + mInnerCycleWithMidCycleMargin +
                mMiddRStrok / 2f;
        if (isStartCanDraging) {
            canvas.drawText("Set Low", maxR, y, midSideBottomTextPaint);
            canvas.drawText(mCurrentCenterBottomShowText, maxR, y + ConvertUtils.dp2px(4) + modeTypeHeight, midSideBottomTextPaint);
        } else if (isEndCanDraging) {
            canvas.drawText("Set High", maxR, y, midSideBottomTextPaint);
            canvas.drawText(mCurrentCenterBottomShowText, maxR, y + ConvertUtils.dp2px(4) + modeTypeHeight, midSideBottomTextPaint);
        } else {
            canvas.drawText(modeType, maxR, y + modeTypeHeight / 2f, midSideBottomTextPaint);
        }
    }

    /**
     * 手势触摸
     *
     * @param event 手势
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        getParent().requestDisallowInterceptTouchEvent(true);
        if (!isEnabled()) {
            return false;
        }
        //兼容
        int actionMasked = MotionEventCompat.getActionMasked(event);
        switch (actionMasked) {
            case MotionEvent.ACTION_DOWN:
                startDragging(event);
                break;
            case MotionEvent.ACTION_MOVE:
                return onDragging(event);
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                stopDragging(event);
                break;
            default:
        }
        return true;
    }

    /**
     * 画开始可滑动的目标指示目标指示刻度
     *
     * @param canvas
     */
    private void drawStartTagView(Canvas canvas) {
        mArcPaint.setColor(getResources().getColor(R.color.color_FFFFFF));
        mArcPaint.setStyle(Paint.Style.FILL);
        mArcPaint.setStrokeWidth(ConvertUtils.dp2px(1f));
        //保存画布坐标
        canvas.save();
        //移动到中心点
        canvas.translate(maxR, maxR);
        //旋转150
        canvas.rotate(-150);
        //开始画圆球路径
        //防止圆球滑动过程中出现按照刻度旋转的情况(按照刻度跳转，不够平滑)
        if (!isStartCanDraging && (mCurrentMode == MODE_TYPE_ON || mCurrentMode == MODE_TYPE_OFF)) {//开始滑动不计算旋转角度
            needRotatteSartAngel = currentStartTag * tempTextAngle;//需要旋转的角度
        }
        //圆球的内容需要更具模式换算
        switch (mCurrentMode) {
            case MODE_TYPE_ON:
            case MODE_TYPE_OFF:
                if (showFan != 0) {
                    canvas.rotate(needRotatteSartAngel);
                    //根据路径画圆球
                    canvas.drawCircle(0, -(mMiddRadias + mMiddRStrok / 2f), slidingBlockRadias, mArcCirclePaint);
                    //画圆球指针
                    mArcPaint.setStrokeWidth(ConvertUtils.dp2px(2f));
                    canvas.drawLine(0, -(mMiddRadias + mMiddRStrok / 2f), 0, (mInnerCycleWithMidCycleMargin + mMiddRStrok) / 2f - mMiddRadias, mArcPaint);
                    //画圆球内容
                    canvas.save();//保存画布
                    // 画布坐标移至滑点中心
                    canvas.translate(0, -(mMiddRadias + mMiddRStrok / 2f));
                    canvas.rotate(150 - needRotatteSartAngel, 0, 0);
                    mArcTextPaint.setTextAlign(Paint.Align.CENTER);
                    mArcTextPaint.setColor(getResources().getColor(R.color.color_15BAFF));
                    mArcTextPaint.setTextSize(slidingBlockRadias);
                    mCurrentCenterBottomShowText = String.valueOf(currentStartShowText);
                    canvas.drawText(String.valueOf(currentStartShowText), 0, caculateTextCenterHeightDistancByBaseLine(mArcTextPaint), mArcTextPaint);
                    canvas.restore();
                }
                break;
            case MODE_TYPE_TIME_TO_OFF:
            case MODE_TYPE_TIME_TO_ON:
                mArcTextPaint.setTextAlign(Paint.Align.CENTER);
                canvas.rotate(needRotatteSartAngel);
                //画开始圆球
                canvas.drawCircle(0, -(mMiddRadias + mMiddRStrok / 2f), slidingBlockRadias, mArcCirclePaint);
                //画开始圆球指针
                mArcPaint.setStrokeWidth(ConvertUtils.dp2px(2f));
                canvas.drawLine(0, -(mMiddRadias + mMiddRStrok / 2f), 0, -mMiddRadias + (mInnerCycleWithMidCycleMargin + mMiddRStrok) / 2f, mArcPaint);
                int tempTime = currentStartShowText;
                //格式化一下显示值
                String s = GuQiangUtil.stringForTimeNoZero(tempTime);
                mCurrentCenterBottomShowText = s;
                mArcTextPaint.setColor(getResources().getColor(R.color.color_15BAFF));
                mArcTextPaint.setTextSize(slidingBlockRadias * 1.2f);
                float startTimeWidth = mArcPaint.measureText(s);
                int startTimeArcWidht = mMiddRadias + ConvertUtils.dp2px(mMiddRStrok) / 2 + ConvertUtils.dp2px(5);
                float startTimeAngle = (float) (((ConvertUtils.dp2px(startTimeWidth) / 2 + ConvertUtils.dp2px(5)) * 180) / (PI * (outCycleRadius + ConvertUtils.dp2px(5))));
                Path startTimePath = new Path();
//                RectF rectStartTime = new RectF(-startTimeArcWidht / 2f, -outCycleRadius + caculateTextCenterHeightDisTance(mArcTextPaint) / 2f, startTimeArcWidht / 2f, -outCycleRadius + ConvertUtils.dp2px(30) + caculateTextCenterHeightDisTance(mArcTextPaint));
                float temp = outCycleRadius - caculateTextCenterHeightDisTance(mArcTextPaint) / 2f;
                RectF rectStartTime = new RectF(-temp, -temp, temp, temp);
                startTimePath.addArc(rectStartTime, -180, 180);
                canvas.drawTextOnPath(s, startTimePath, 0, 0, mArcTextPaint);
                break;
            case MODE_TYPE_AUTO_HUM:
                mArcPaint.setStrokeWidth(ConvertUtils.dp2px(2f));
                mArcTextPaint.setTextAlign(Paint.Align.CENTER);
                mArcTextPaint.setTextSize(slidingBlockRadias);
                mArcTextPaint.setColor(getResources().getColor(R.color.color_5CC8FF));
                canvas.rotate(needRotatteEndAngel);
                //画结束圆球
                canvas.drawCircle(0, -outCycleRadius, slidingBlockRadias, mArcCirclePaint);
                //画开始圆球指针
                canvas.drawLine(0, -outCycleRadius, 0, -mMiddRadias + (mInnerCycleWithMidCycleMargin + mMiddRStrok) / 2f, mArcPaint);
                //画开始圆球内容
                canvas.save();//保存画布
                canvas.translate(0, -outCycleRadius);
                canvas.rotate(150 - needRotatteEndAngel, 0, 0);
                if (isEndCanDraging) {
                    mCurrentCenterBottomShowText = currentEndShowText + "%";
                }
                canvas.drawText(String.valueOf(currentEndShowText), 0, caculateTextCenterHeightDistancByBaseLine(mArcTextPaint), mArcTextPaint);
                canvas.restore();
                canvas.rotate(needRotatteSartAngel - needRotatteEndAngel);
                //画开始圆球
                canvas.drawCircle(0, -(mMiddRadias + mMiddRStrok / 2f), slidingBlockRadias, mArcCirclePaint);
                //画开始圆球指针
                mArcPaint.setStrokeWidth(ConvertUtils.dp2px(2f));
                canvas.drawLine(0, -(mMiddRadias + mMiddRStrok / 2f), 0, -mMiddRadias + (mInnerCycleWithMidCycleMargin + mMiddRStrok) / 2f, mArcPaint);
                //画开始圆球内容
                canvas.save();//保存画布
                canvas.translate(0, -(mMiddRadias + mMiddRStrok / 2f));
                canvas.rotate(150 - needRotatteSartAngel, 0, 0);
                if (isStartCanDraging) {
                    mCurrentCenterBottomShowText = currentStartShowText + "%";
                }
                canvas.drawText(String.valueOf(currentStartShowText), 0, caculateTextCenterHeightDistancByBaseLine(mArcTextPaint), mArcTextPaint);
                canvas.restore();
                break;
            case MODE_TYPE_AUTO_TMP_C:
                mArcPaint.setStrokeWidth(ConvertUtils.dp2px(2f));
                mArcTextPaint.setTextAlign(Paint.Align.CENTER);
                mArcTextPaint.setTextSize(slidingBlockRadias);
                mArcTextPaint.setColor(getResources().getColor(R.color.color_5CC8FF));
                canvas.rotate(needRotatteEndAngel);
                //画结束圆球
                canvas.drawCircle(0, -outCycleRadius, slidingBlockRadias, mArcCirclePaint);
                //画开始圆球指针
                canvas.drawLine(0, -outCycleRadius, 0, -mMiddRadias + (mInnerCycleWithMidCycleMargin + mMiddRStrok) / 2f, mArcPaint);
                //画开始圆球内容
                canvas.save();//保存画布
                canvas.translate(0, -outCycleRadius);
                canvas.rotate(150 - needRotatteEndAngel, 0, 0);
                if (isEndCanDraging) {
                    mCurrentCenterBottomShowText = currentEndShowText + DEGREE;
                }
                canvas.drawText(String.valueOf(currentEndShowText), 0, caculateTextCenterHeightDistancByBaseLine(mArcTextPaint), mArcTextPaint);
                canvas.restore();
                canvas.rotate(needRotatteSartAngel - needRotatteEndAngel);
                //画开始圆球
                canvas.drawCircle(0, -(mMiddRadias + mMiddRStrok / 2f), slidingBlockRadias, mArcCirclePaint);
                //画开始圆球指针
                canvas.drawLine(0, -(mMiddRadias + mMiddRStrok / 2f), 0, -mMiddRadias + (mInnerCycleWithMidCycleMargin + mMiddRStrok) / 2f, mArcPaint);
                //画开始圆球内容
                canvas.save();//保存画布
                canvas.translate(0, -(mMiddRadias + mMiddRStrok / 2f));
                canvas.rotate(150 - needRotatteSartAngel, 0, 0);
                if (isStartCanDraging) {
                    mCurrentCenterBottomShowText = currentStartShowText + DEGREE;
                }
                canvas.drawText(String.valueOf(currentStartShowText), 0, caculateTextCenterHeightDistancByBaseLine(mArcTextPaint), mArcTextPaint);
                canvas.restore();
                break;
            case MODE_TYPE_AUTO_TMP_F:
                mArcPaint.setStrokeWidth(ConvertUtils.dp2px(2f));
                mArcTextPaint.setTextAlign(Paint.Align.CENTER);
                mArcTextPaint.setTextSize(slidingBlockRadias);
                mArcTextPaint.setColor(getResources().getColor(R.color.color_5CC8FF));
                canvas.rotate(needRotatteEndAngel);
                //画结束圆球
                canvas.drawCircle(0, -outCycleRadius, slidingBlockRadias, mArcCirclePaint);
                //画开始圆球指针
                canvas.drawLine(0, -outCycleRadius, 0, -mMiddRadias + (mInnerCycleWithMidCycleMargin + mMiddRStrok) / 2f, mArcPaint);
                //画开始圆球内容
                canvas.save();//保存画布
                canvas.translate(0, -outCycleRadius);
                canvas.rotate(150 - needRotatteEndAngel, 0, 0);
                int endFahren = currentEndShowText + 32;
                if (isEndCanDraging) {
                    mCurrentCenterBottomShowText = endFahren + FAH;
                }
                canvas.drawText(String.valueOf(endFahren), 0, caculateTextCenterHeightDistancByBaseLine(mArcTextPaint), mArcTextPaint);
                canvas.restore();
                canvas.rotate(needRotatteSartAngel - needRotatteEndAngel);
                //画开始圆球
                canvas.drawCircle(0, -(mMiddRadias + mMiddRStrok / 2f), slidingBlockRadias, mArcCirclePaint);
                //画开始圆球指针
                mArcPaint.setStrokeWidth(ConvertUtils.dp2px(2f));
                canvas.drawLine(0, -(mMiddRadias + mMiddRStrok / 2f), 0, -mMiddRadias + (mInnerCycleWithMidCycleMargin + mMiddRStrok) / 2f, mArcPaint);
                //画开始圆球内容
                canvas.save();//保存画布
                canvas.translate(0, -(mMiddRadias + mMiddRStrok / 2f));
                canvas.rotate(150 - needRotatteSartAngel, 0, 0);
                int startFahren = currentStartShowText + 32;
                if (isStartCanDraging) {
                    mCurrentCenterBottomShowText = startFahren + FAH;
                }
                canvas.drawText(String.valueOf(startFahren), 0, caculateTextCenterHeightDistancByBaseLine(mArcTextPaint), mArcTextPaint);
                canvas.restore();

                break;
            case MODE_TYPE_CYCLE:
                canvas.rotate(needRotatteSartAngel);
                //画开始圆球
                canvas.drawCircle(0, -(mMiddRadias + mMiddRStrok / 2f), slidingBlockRadias, mArcCirclePaint);
                //画开始圆球指针
                mArcPaint.setStrokeWidth(ConvertUtils.dp2px(2f));
                canvas.drawLine(0, -(mMiddRadias + mMiddRStrok / 2f), 0, -mMiddRadias + (mInnerCycleWithMidCycleMargin + mMiddRStrok) / 2f, mArcPaint);
                //画开始圆球内容
                canvas.save();//保存画布
                // 画布坐标移至滑点中心
                canvas.translate(0, -(mMiddRadias + mMiddRStrok / 2f));
                canvas.rotate(150 - needRotatteSartAngel, 0, 0);
                mArcTextPaint.setTextAlign(Paint.Align.CENTER);
                mArcTextPaint.setColor(getResources().getColor(R.color.color_15BAFF));
                mArcTextPaint.setTextSize(slidingBlockRadias);
                canvas.drawText("ON", 0, caculateTextCenterHeightDistancByBaseLine(mArcTextPaint), mArcTextPaint);
                canvas.restore();
//                int tempCycleStartTime = currentStartShowText * 5;
                int tempCycleStartTime = currentStartShowText;
                //格式化一下显示值
                String cycleTime = GuQiangUtil.stringForTimeNoZero(tempCycleStartTime);
                mArcTextPaint.setColor(getResources().getColor(R.color.color_01b5ea));
                mArcTextPaint.setTextSize(slidingBlockRadias * 1.2f);
                if (isStartCanDraging) {
                    mCurrentCenterBottomShowText = cycleTime;
                }
                float cycleStartTimeWidth = mArcPaint.measureText(cycleTime);
                int cycleStartTimeArcWidht = mMiddRadias + mMiddRStrok / 2 + ConvertUtils.dp2px(5);
                float cycleStartTimeAngle = (float) (((ConvertUtils.dp2px(cycleStartTimeWidth) / 2 + ConvertUtils.dp2px(5)) * 180) / (PI * (outCycleRadius + ConvertUtils.dp2px(5))));
                Path cycleStartTimePath = new Path();
                canvas.rotate(cycleStartTimeAngle);
                RectF cycleRectStartTime = new RectF(-cycleStartTimeArcWidht / 2f, -outCycleRadius + caculateTextCenterHeightDisTance(mArcTextPaint) / 2f, cycleStartTimeArcWidht / 2f, -outCycleRadius + ConvertUtils.dp2px(30) + caculateTextCenterHeightDisTance(mArcTextPaint));
                cycleStartTimePath.addArc(cycleRectStartTime, -180, 180);
                canvas.drawTextOnPath(cycleTime, cycleStartTimePath, -cycleStartTimeWidth / 2 - ConvertUtils.dp2px(13), 0, mArcTextPaint);
                canvas.rotate(-cycleStartTimeAngle);
//                drawTextByCyclePoint(canvas, cycleTime, needRotatteSartAngel);
//                canvas.drawText(cycleTime, 0, -(mMiddRadias + ConvertUtils.dp2px(mMiddRStrok) / 2 + ConvertUtils.dp2px(7)), mArcTextPaint);
                canvas.restore();
                canvas.save();
                //移动到中心点
                canvas.translate(maxR, maxR);
                //旋转150
                canvas.rotate(-150);
                canvas.rotate(158);
                //画结束圆球
                canvas.rotate(needRotatteEndAngel);
                //画开始圆球
                canvas.drawCircle(0, -(mMiddRadias + mMiddRStrok / 2f), slidingBlockRadias, mArcCirclePaint);
                //画开始圆球指针
                canvas.drawLine(0, -(mMiddRadias + mMiddRStrok / 2f), 0, -mMiddRadias + (mInnerCycleWithMidCycleMargin + mMiddRStrok) / 2f, mArcPaint);
                //画开始圆球内容
                canvas.save();//保存画布
                // 画布坐标移至滑点中心
                canvas.translate(0, -(mMiddRadias + mMiddRStrok / 2f));
                canvas.rotate(-(needRotatteEndAngel + 8), 0, 0);
                mArcTextPaint.setTextAlign(Paint.Align.CENTER);
                mArcTextPaint.setColor(getResources().getColor(R.color.color_15BAFF));
                mArcTextPaint.setTextSize(slidingBlockRadias);
                canvas.drawText("OFF", 0, caculateTextCenterHeightDistancByBaseLine(mArcTextPaint), mArcTextPaint);
                canvas.restore();
//                int tempCycleEndTime = currentEndShowText * 5;
                int tempCycleEndTime = currentEndShowText;
                //格式化一下显示值
                String cycleEndTime = GuQiangUtil.stringForTimeNoZero(tempCycleEndTime);
                if (isEndCanDraging) {
                    mCurrentCenterBottomShowText = cycleEndTime;
                }
                mArcTextPaint.setColor(getResources().getColor(R.color.color_01b5ea));
                mArcTextPaint.setTextSize(slidingBlockRadias * 1.2f);
                float cycleEndTimeWidth = mArcPaint.measureText(cycleEndTime);
                int cycleEndTimeArcWidht = mMiddRadias + mMiddRStrok / 2 + ConvertUtils.dp2px(5);
                float cycleEndTimeAngle = (float) (((ConvertUtils.dp2px(cycleEndTimeWidth) / 2 + ConvertUtils.dp2px(5)) * 180) / (PI * (outCycleRadius + ConvertUtils.dp2px(5))));
                Path cycleEndTimePath = new Path();
                canvas.rotate(cycleEndTimeAngle);
                RectF cycleRectEndTime = new RectF(-cycleEndTimeArcWidht / 2f, -outCycleRadius + caculateTextCenterHeightDisTance(mArcTextPaint) / 2f, cycleEndTimeArcWidht / 2, -outCycleRadius + ConvertUtils.dp2px(30) + caculateTextCenterHeightDisTance(mArcTextPaint));
                cycleEndTimePath.addArc(cycleRectEndTime, -180, 180);
                canvas.drawTextOnPath(cycleEndTime, cycleEndTimePath, -cycleEndTimeWidth / 2 - ConvertUtils.dp2px(13), 0, mArcTextPaint);
                canvas.rotate(-cycleEndTimeAngle);
                break;
            case MODE_TYPE_SCHEDULE:
                //画结束圆球
                mArcPaint.setStrokeWidth(ConvertUtils.dp2px(2f));
                canvas.rotate(needRotatteEndAngel);
                canvas.drawCircle(0, -outCycleRadius, slidingBlockRadias, mArcCirclePaint);
                //画结束圆球指针
                canvas.drawLine(0, -outCycleRadius, 0, -mMiddRadias + (mInnerCycleWithMidCycleMargin + mMiddRStrok) / 2f, mArcPaint);
                //画开始圆球内容
                canvas.save();//保存画布
                canvas.translate(0, -outCycleRadius);
                canvas.rotate(150 - needRotatteEndAngel, 0, 0);
                mArcTextPaint.setTextAlign(Paint.Align.CENTER);
                mArcTextPaint.setTextSize(slidingBlockRadias * 0.9f);
                mArcTextPaint.setColor(getResources().getColor(R.color.color_5CC8FF));
                canvas.drawText("END", 0, caculateTextCenterHeightDistancByBaseLine(mArcTextPaint), mArcTextPaint);
                canvas.restore();
                mArcTextPaint.setTextSize(slidingBlockRadias * 1.2f);
                String endSchedTime = GuQiangUtil.stringForTimeByAmPm(currentEndShowText);
                if (isEndCanDraging) {
                    mCurrentCenterBottomShowText = endSchedTime;
                }
                //结束文字长度
                float textEndWidth = mArcTextPaint.measureText(endSchedTime);
                //结束文字需要旋转的角度
                Path endPath = new Path();
                int radius = outCycleRadius + ConvertUtils.dp2px(3);
                RectF rectEnd = new RectF(-radius, -radius, radius, radius);
                endPath.addArc(rectEnd, -180, 180);
                canvas.drawTextOnPath(endSchedTime, endPath, textEndWidth / 2f + slidingBlockRadias + ConvertUtils.dp2px(2), 0, mArcTextPaint);
//                //旋转画布画开始点
//                //画开始圆球
                canvas.rotate(needRotatteSartAngel - needRotatteEndAngel);
                canvas.drawCircle(0, -(mMiddRadias + mMiddRStrok / 2f), slidingBlockRadias, mArcCirclePaint);
                //画开始圆球指针
                mArcPaint.setStrokeWidth(ConvertUtils.dp2px(2f));
                canvas.drawLine(0, -(mMiddRadias + mMiddRStrok / 2f), 0, -mMiddRadias + (mInnerCycleWithMidCycleMargin + mMiddRStrok) / 2f, mArcPaint);
                //画开始圆球内容
                canvas.save();//保存画布
                canvas.translate(0, -(mMiddRadias + mMiddRStrok / 2f));
                canvas.rotate(150 - needRotatteSartAngel, 0, 0);
                mArcTextPaint.setTextAlign(Paint.Align.CENTER);
                mArcTextPaint.setTextSize(slidingBlockRadias * 0.9f);
                mArcTextPaint.setColor(getResources().getColor(R.color.color_5CC8FF));
                canvas.drawText("SRT", 0, caculateTextCenterHeightDistancByBaseLine(mArcTextPaint), mArcTextPaint);
                canvas.restore();
                //画开始圆球内容
                //画开始圆球时间
                mArcTextPaint.setTextSize(slidingBlockRadias * 1.2f);
                String startSchedTime = GuQiangUtil.stringForTimeByAmPm(currentStartShowText);
                if (isStartCanDraging) {
                    mCurrentCenterBottomShowText = startSchedTime;
                }
                float textStartWidth = mArcTextPaint.measureText(startSchedTime);
                Path path = new Path();
                radius = -(mMiddRadias + mMiddRStrok / 2 + ConvertUtils.dp2px(3));
                RectF rectF = new RectF(radius, radius, -radius, -radius);
                mArcTextPaint.setColor(getResources().getColor(R.color.color_5CC8FF));
                path.addArc(rectF, -180, 180);
                canvas.drawTextOnPath(startSchedTime, path, -textStartWidth / 2f - slidingBlockRadias - ConvertUtils.dp2px(2), 0, mArcTextPaint);
                break;
            default:
        }
        //恢复画布坐标
        canvas.restore();
    }

    public void setStart(boolean b) {
        startClose = !b;
        invalidate();
    }

    public void setEnd(boolean b) {
        endClose = !b;
        invalidate();
    }

    public boolean getStart() {
        return !startClose;
    }

    public boolean getEnd() {
        return !endClose;
    }

    /**
     * 画圆弧
     *
     * @param canvas 画布
     */
    private void drawArc(Canvas canvas) {
        mArcPaint.setColor(getResources().getColor(R.color.color_80FFFFFF));
        mArcPaint.setStyle(Paint.Style.FILL);
        mArcPaint.setStrokeWidth(ConvertUtils.dp2px(1f));
        canvas.save();
        canvas.translate(maxR, maxR);
        canvas.rotate(-150f);
        int half = (int) (0.9375 * slidingBlockRadias);
        int distance = (int) (0.3125 * slidingBlockRadias);
        if (mCurrentMode == MODE_TYPE_CYCLE) {//循环模式
            tempAngle = (float) 142 / maxTotal;//指针滑动最小角度
            tempTextAngle = (float) 142 / realMaxTotal;//计算读数最小角度
            for (int i = 0; i < maxTotal + 1; i++) {
                float currentRoateAngel = tempAngle * i;
                mArcPaint.setColor(getResources().getColor(currentRoateAngel < needRotatteSartAngel ? R.color.color_FFFFFF : R.color.color_80FFFFFF));
                canvas.drawLine(0, -mMiddRadias - (i == 0 || i == maxTotal ? half + distance : half - distance), 0, -mMiddRadias + half + distance, mArcPaint);
                canvas.rotate(tempAngle);
            }
            canvas.restore();
            canvas.save();
            canvas.translate(maxR, maxR);
            canvas.rotate(-150f);
            canvas.rotate(158);
            for (int i = 0; i < maxTotal + 1; i++) {
                float currentRoateAngel = tempAngle * i;
                mArcPaint.setColor(getResources().getColor(currentRoateAngel < needRotatteEndAngel ? R.color.color_FFFFFF : R.color.color_80FFFFFF));
                canvas.drawLine(0, -mMiddRadias - (i == 0 || i == maxTotal ? half + distance : half - distance), 0, -mMiddRadias + half + distance, mArcPaint);
                canvas.rotate(tempAngle);
            }
        } else {
            tempAngle = (float) 300 / maxTotal;//指针滑动最小角度
            tempTextAngle = (float) 300 / realMaxTotal;//计算读数最小角度
            for (int i = 0; i < maxTotal + 1; i++) {
                float currentRoateAngel = tempAngle * i;
                switch (mCurrentMode) {
                    case MODE_TYPE_ON:
                    case MODE_TYPE_OFF:
                    case MODE_TYPE_TIME_TO_OFF:
                    case MODE_TYPE_TIME_TO_ON:
                        mArcPaint.setColor(getResources().getColor(currentRoateAngel < needRotatteSartAngel ? R.color.color_FFFFFF : R.color.color_80FFFFFF));
                        break;
                    case MODE_TYPE_AUTO_HUM:
                    case MODE_TYPE_AUTO_TMP_F:
                    case MODE_TYPE_AUTO_TMP_C:
                        float startAngel = startClose ? 0 : needRotatteSartAngel;
                        float endAngel = endClose ? 300 : needRotatteEndAngel;
                        if (startAngel >= endAngel) {//如果低温高于或者等于高温 中间这段刻度线为白色    update 高低温和高低湿相同时，将显示两边区域，改为：显示中间区域    update：边缘需要特殊处理
                            int color_res = R.color.color_80FFFFFF;
                            if (currentRoateAngel >= endAngel && currentRoateAngel <= startAngel || (startClose && !endClose && endAngel == 0) || (!startClose && endClose && startAngel == 300)) {
                                color_res = R.color.color_FFFFFF;
                            }
                            mArcPaint.setColor(getResources().getColor(color_res));
                        } else {//低温小于高温
                            mArcPaint.setColor(getResources().getColor(currentRoateAngel >= startAngel && currentRoateAngel <= endAngel ? R.color.color_80FFFFFF : R.color.color_FFFFFF));
                        }
                        break;
                    case MODE_TYPE_SCHEDULE:
                        if (needRotatteSartAngel <= needRotatteEndAngel) {//开始时间小于结束时间
//                            mArcPaint.setColor(getResources().getColor(currentRoateAngel > needRotatteEndAngel && currentRoateAngel < needRotatteSartAngel ? R.color.color_FFFFFF : R.color.color_80FFFFFF));
                            mArcPaint.setColor(getResources().getColor(currentRoateAngel > needRotatteSartAngel && currentRoateAngel < needRotatteEndAngel ? R.color.color_FFFFFF : R.color.color_80FFFFFF));
//                            mArcPaint.setStrokeWidth(ConvertUtils.dp2px(currentRoateAngel > needRotatteEndAngel && currentRoateAngel < needRotatteSartAngel ? 2f : 1f));
                        } else {//开始时间大于结束时间
                            mArcPaint.setColor(getResources().getColor(currentRoateAngel < needRotatteSartAngel && currentRoateAngel > needRotatteEndAngel ? R.color.color_80FFFFFF : R.color.color_FFFFFF));
//                            mArcPaint.setStrokeWidth(ConvertUtils.dp2px(currentRoateAngel > needRotatteSartAngel && currentRoateAngel < needRotatteEndAngel ? 1f : 2f));
                        }
                        break;
                }
                canvas.drawLine(0, -mMiddRadias - (i == 0 || i == maxTotal ? half + distance : half - distance), 0, -mMiddRadias + half + distance, mArcPaint);
                canvas.rotate(tempAngle);
            }
        }
        canvas.restore();
    }

    /**
     * 设置表盘模式
     *
     * @param modeType
     */
    public void setModeType(int modeType) {
        mCurrentMode = modeType;
        switch (mCurrentMode) {
            case MODE_TYPE_ON:
            case MODE_TYPE_OFF:
                maxTotal = 10;
                realMaxTotal = 10;
//                initStartOrEndAngle(0, 0, 300, 300);
                break;
            case MODE_TYPE_TIME_TO_OFF:
            case MODE_TYPE_TIME_TO_ON:
            case MODE_TYPE_SCHEDULE:
                maxTotal = 48;
                realMaxTotal = 1440;
                break;
            case MODE_TYPE_AUTO_HUM:
                maxTotal = 50;
                realMaxTotal = 100;
                break;
            case MODE_TYPE_AUTO_TMP_C:
                maxTotal = 45;
                realMaxTotal = 90;
                break;
            case MODE_TYPE_AUTO_TMP_F:
                maxTotal = 54;
                realMaxTotal = 162;
                break;
            case MODE_TYPE_CYCLE:
                maxTotal = 24;
                realMaxTotal = 1440;
                break;
            default:
                break;
        }
        invalidate();
    }


    /**
     * 根据On Off模式画内圆数据
     */
    private void drawInnterTextByOnOrOff(Canvas canvas, String TmpOrHum, String TmepOrHumUnit, String time, String fanSpeed) {
        //画温度
        mInnerPaint.setColor(Color.WHITE);
        mInnerPaint.setTextSize(mInnerR / 4f);
        mInnerPaint.setAlpha(mAlpha);
        float mInnerTopTextWidth = mInnerPaint.measureText(TmpOrHum);
        canvas.drawText(TmpOrHum, maxR - mInnerTopTextWidth / 2, maxR - mInnerR + ConvertUtils.dp2px(8) + caculateTextCenterHeightDisTance(mInnerPaint), mInnerPaint);
        //画温度单位
        mInnerPaint.setTextSize(mInnerR * 0.75f / 4);
        canvas.drawText(TmepOrHumUnit, maxR + mInnerTopTextWidth / 2, maxR - mInnerR + ConvertUtils.dp2px(8) + caculateTextCenterHeightDisTance(mInnerPaint), mInnerPaint);
        if (showFan != 0) {
            drawCenterFan(canvas);
        } else {
            drawCenterFanModel(canvas);
        }
        //画底部时间文字
        mInnerPaint.setColor(Color.WHITE);
        mInnerPaint.setTextSize(mInnerR * 0.875f / 4);
        float mInnerBottomTimeTextWidth = mInnerPaint.measureText(time);
        float mBottomTimeCaculeteBaselin = caculateTextCenterHeightDistancByBaseLine(mInnerPaint);
        canvas.drawText(time, maxR - mInnerBottomTimeTextWidth / 2, maxR + mInnerR - ConvertUtils.dp2px(9), mInnerPaint);
    }

    private void drawCenterFan(Canvas canvas) {
        //画中心速度文字
        mInnerPaint.setAlpha(255);
        mInnerPaint.setColor(getResources().getColor(R.color.color_FF7800));
        mInnerPaint.setTextSize(innerTmpHumTextSize);
        float mInnerCenterTextWidth = mInnerPaint.measureText(innerCircleSpeed);
        float mInnerCenterTextBaseline = caculateTextCenterHeightDistancByBaseLine(mInnerPaint);
        canvas.drawText(innerCircleSpeed, maxR - mInnerCenterTextWidth / 2, maxR + mInnerCenterTextBaseline, mInnerPaint);
        //画中心风速和上下图标
        Bitmap fanNormal = BitmapFactory.decodeResource(getResources(), showFan == 2 ? R.mipmap.control_cicle : R.mipmap.biapan_off_fengshan);
        int fanNormalWidth = fanNormal.getWidth();
        int fanNormalHeight = fanNormal.getHeight();
        Matrix matrix = new Matrix();
        mInnerCenterTextBaseline = innerTmpHumTextSize * 0.4f / fanNormalWidth;
        matrix.setScale(mInnerCenterTextBaseline, mInnerCenterTextBaseline);
        fanNormal = Bitmap.createBitmap(fanNormal, 0, 0, fanNormalWidth, fanNormalHeight, matrix, true);
        fanNormalWidth = fanNormal.getWidth();
        fanNormalHeight = fanNormal.getHeight();
        float fanLeft = maxR - mInnerCenterTextWidth / 2 - fanNormalWidth;
        float fanTop = maxR - fanNormalHeight / 2;
        canvas.drawBitmap(fanNormal, null, new RectF(fanLeft, fanTop, fanLeft + fanNormalWidth, fanTop + fanNormalHeight), mInnerPaint);
        Bitmap upOrDown = getOffStateBitmap();
        int upOrDownWidth = upOrDown.getWidth();
        int upOrDownHeight = upOrDown.getHeight();
        mInnerCenterTextBaseline = innerTmpHumTextSize * 0.4f / upOrDownWidth;
        matrix.setScale(mInnerCenterTextBaseline, mInnerCenterTextBaseline);
        upOrDown = Bitmap.createBitmap(upOrDown, 0, 0, upOrDownWidth, upOrDownHeight, matrix, true);
        upOrDownWidth = upOrDown.getWidth();
        upOrDownHeight = upOrDown.getHeight();
        float upOrDownLeft = maxR + mInnerCenterTextWidth / 2;
        float upOrDownTop = maxR - upOrDownHeight / 2f;
        canvas.drawBitmap(upOrDown, null, new RectF(upOrDownLeft, upOrDownTop, upOrDownLeft + upOrDownWidth, upOrDownTop + upOrDownHeight), mInnerPaint);
    }

    private void drawCenterFanModel(Canvas canvas) {
        if (mCurrentMode == MODE_TYPE_ON) {
            innerCircleSpeed = "ON";
        } else {
            innerCircleSpeed = "OFF";
        }
        //画中心文字
        mInnerPaint.setAlpha(255);
        mInnerPaint.setColor(getResources().getColor(R.color.color_FF7800));
        mInnerPaint.setTextSize(innerTmpHumTextSize);
        float mInnerCenterTextWidth = mInnerPaint.measureText(innerCircleSpeed);
        float mInnerCenterTextBaseline = caculateTextCenterHeightDistancByBaseLine(mInnerPaint);
        canvas.drawText(innerCircleSpeed, maxR - mInnerCenterTextWidth / 2, maxR + mInnerCenterTextBaseline, mInnerPaint);
    }

    Bitmap getOffStateBitmap() {
        Bitmap result;
        switch (innerState) {
            case 0x0:
                result = BitmapFactory.decodeResource(getResources(), R.mipmap.control_stead_trend);
                break;
            case 0x1:
                result = BitmapFactory.decodeResource(getResources(), R.mipmap.control_off_decrease_trend);
                break;
            default:
                result = BitmapFactory.decodeResource(getResources(), R.mipmap.control_off_increase_trend);
        }
        return result;
    }

    Bitmap getAutoStateBitmap() {
        Bitmap result;
        switch (innerState) {
            case 0x0:
                result = BitmapFactory.decodeResource(getResources(), R.mipmap.control_stead_trend);
                break;
            case 0x1:
                result = BitmapFactory.decodeResource(getResources(), R.mipmap.control_auto_decrease_trend);
                break;
            default:
                result = BitmapFactory.decodeResource(getResources(), R.mipmap.control_auto_increase_trend);
        }
        return result;
    }

    /**
     * 根据Auto模式画内圆数据
     *
     * @param canvas
     * @param time
     * @param TmpOrHum
     * @param TmepOrHumUnit
     * @param fanSpeed
     */
    private void drawInnerTextByAuto(Canvas canvas, String time, String TmpOrHum, String TmepOrHumUnit, String fanSpeed) {
        mInnerPaint.setColor(Color.WHITE);
        mInnerPaint.setTextSize(innerTmpHumTextSize * 0.25f);
        float mInnerBottomTimeTextWidth = mInnerPaint.measureText(time);
        float mBottomTimeCaculeteBaselin = caculateTextCenterHeightDistancByBaseLine(mInnerPaint);
        if (!isDeviceC) {       //C家族不画顶部时间文字
            canvas.drawText(time, maxR - mInnerBottomTimeTextWidth / 2, maxR - mInnerR + ConvertUtils.dp2px(20) + mBottomTimeCaculeteBaselin, mInnerPaint);
        }
        Bitmap upOrDown = getAutoStateBitmap();
        int upOrDownWidth = upOrDown.getWidth();
        int upOrDownHeight = upOrDown.getHeight();
        Matrix matrix = new Matrix();
        mBottomTimeCaculeteBaselin = innerTmpHumTextSize * 0.25f / upOrDownWidth;
        matrix.setScale(mBottomTimeCaculeteBaselin, mBottomTimeCaculeteBaselin);
        upOrDown = Bitmap.createBitmap(upOrDown, 0, 0, upOrDownWidth, upOrDownHeight, matrix, true);
        upOrDownWidth = upOrDown.getWidth();
        upOrDownHeight = upOrDown.getHeight();
        //画中心温度
        mInnerPaint.setColor(getResources().getColor(R.color.color_FF7800));
        mInnerPaint.setTextSize(innerTmpHumTextSize);
        mInnerPaint.setAlpha(mAlpha);
        float mInnerCenterTextWidth = mInnerPaint.measureText(TmpOrHum);
        float mInnerCenterTextBaseline = caculateTextCenterHeightDistancByBaseLine(mInnerPaint);
        int mInnerCenterTextHeight = caculateTextCenterHeightDisTance(mInnerPaint);
        canvas.drawText(TmpOrHum, maxR - (mInnerCenterTextWidth + upOrDownWidth) / 2, maxR + mInnerCenterTextBaseline, mInnerPaint);
        //画温度单位
        mInnerPaint.setTextSize(innerTmpHumTextSize * 0.25f);
        canvas.drawText(TmepOrHumUnit, maxR + (mInnerCenterTextWidth - upOrDownWidth) / 2, maxR - mInnerCenterTextHeight / 2f + caculateTextCenterHeightDisTance(mInnerPaint), mInnerPaint);
//        //画当前温度上升和下降
        mInnerPaint.setAlpha(255);
        float upOrDownLeft = maxR + (mInnerCenterTextWidth - upOrDownWidth) / 2;
        float upOrDownTop = maxR + mInnerCenterTextHeight / 2f - upOrDownHeight;
        canvas.drawBitmap(upOrDown, null, new RectF(upOrDownLeft, upOrDownTop, upOrDownLeft + upOrDownWidth, upOrDownTop + upOrDownHeight), mInnerPaint);
//        //画底部风速和风速读数
        if (showFan != 0) {
            Bitmap fanNormal = BitmapFactory.decodeResource(getResources(), showFan == 2 ? R.mipmap.control_cicle_small : R.mipmap.kongzhi_fengshan_icon);
            int fanNormalWidth = fanNormal.getWidth();
            int fanNormalHeight = fanNormal.getHeight();
            float fanLeft = maxR - fanNormalWidth;
            float fanTop = maxR + mInnerR - ConvertUtils.dp2px(6) - fanNormalHeight;
            canvas.drawBitmap(fanNormal, null, new RectF(fanLeft, fanTop, fanLeft + fanNormalWidth, fanTop + fanNormalHeight), mInnerPaint);
            mInnerPaint.setTextSize(mInnerR / 4f);
            mInnerPaint.setColor(Color.WHITE);
            canvas.drawText(fanSpeed, maxR + ConvertUtils.dp2px(5), maxR + mInnerR - ConvertUtils.dp2px(8), mInnerPaint);
        } else if (showOffOrOn) { // 画 OFF 或者 ON
            mInnerPaint.setTextSize(mInnerR / 4f);
            mInnerPaint.setColor(Color.WHITE);
            String s = fanSpeed.equals("0") ? "OFF" : "ON";
            canvas.drawText(s, maxR - mInnerPaint.measureText(s) / 2, maxR + mInnerR - ConvertUtils.dp2px(8), mInnerPaint);
        }
    }

    /**
     * 根据TimeToOn/Off模式画内圆数据
     * 9
     *
     * @param canvas        画布
     * @param time          当前时间
     * @param TmpOrHum      当前温湿度
     * @param TmepOrHumUnit 当前温湿度单位
     * @param fanSpeed      当前风扇转速
     * @param currentMode   当前模式 to on  还是 to Off
     */
    private void drawInnerTextByTimeToOn(Canvas canvas, String time, String TmpOrHum, String TmepOrHumUnit, String fanSpeed, String currentMode) {
        //画底部风速和风速读数
        if (showFan != 0) {
            Bitmap fanNormal = BitmapFactory.decodeResource(getResources(), showFan == 2 ? R.mipmap.control_cicle_small : R.mipmap.kongzhi_fengshan_icon);
            int fanNormalWidth = fanNormal.getWidth();
            int fanNormalHeight = fanNormal.getHeight();
            Matrix matrix = new Matrix();
            matrix.setScale(fanNormalWidth * 4f / mInnerR, fanNormalHeight * 4f / mInnerR);
            Bitmap.createBitmap(fanNormal, 0, 0, fanNormalWidth, fanNormalHeight, matrix, true);
            fanNormalWidth = fanNormal.getWidth();
            fanNormalHeight = fanNormal.getHeight();
            float fanLeft = maxR - fanNormalWidth;
            float fanTop = maxR - mInnerR + ConvertUtils.dp2px(6);
            canvas.drawBitmap(fanNormal, null, new RectF(fanLeft, fanTop, fanLeft + fanNormalWidth, fanTop + fanNormalHeight), mInnerPaint);
            mInnerPaint.setTextSize(mInnerR / 4f);
            mInnerPaint.setColor(Color.WHITE);
            canvas.drawText(fanSpeed, maxR + ConvertUtils.dp2px(4), maxR - mInnerR + ConvertUtils.dp2px(6) + caculateTextCenterHeightDisTance(mInnerPaint), mInnerPaint);
        } else if (showOffOrOn) {// 画 OFF 或者 ON
            mInnerPaint.setTextSize(mInnerR / 4f);
            mInnerPaint.setColor(Color.WHITE);
            String s = fanSpeed.equals("0") ? "OFF" : "ON";
            canvas.drawText(s, maxR - mInnerPaint.measureText(s) / 2, maxR - mInnerR + ConvertUtils.dp2px(6) + caculateTextCenterHeightDisTance(mInnerPaint), mInnerPaint);
        }
        //画中心数值 和 模式
        mInnerPaint.setTextSize(innerTimeTextSize);
        mInnerPaint.setColor(getResources().getColor(R.color.color_FF7800));
        float mCenterTimeWidth = mInnerPaint.measureText(time);
        float mCenterTimeY = maxR + caculateTextCenterHeightDisTance(mInnerPaint) / 2f - ConvertUtils.dp2px(10);
        canvas.drawText(time, maxR - mCenterTimeWidth / 2, mCenterTimeY, mInnerPaint);
        mInnerPaint.setTextSize(innerTimeTextSize / 2);
        float mCenterModeWidth = mInnerPaint.measureText(currentMode);
        canvas.drawText(currentMode, maxR - mCenterModeWidth / 2, mCenterTimeY + caculateTextCenterHeightDisTance(mInnerPaint) + ConvertUtils.dp2px(5), mInnerPaint);
        //画底部温度和温度单位
        mInnerPaint.setColor(Color.WHITE);
        mInnerPaint.setTextSize(mInnerR / 4f);
        mInnerPaint.setAlpha(mAlpha);
        float mInnerBottomTextWidth = mInnerPaint.measureText(TmpOrHum);
        int mInnerBottomTextHeight = caculateTextCenterHeightDisTance(mInnerPaint);
        canvas.drawText(TmpOrHum, maxR - mInnerBottomTextWidth / 2, maxR + mInnerR - ConvertUtils.dp2px(8), mInnerPaint);
        //画温度单位
        mInnerPaint.setTextSize(mInnerR / 8f);
        canvas.drawText(TmepOrHumUnit, maxR + mInnerBottomTextWidth / 2, maxR + mInnerR - ConvertUtils.dp2px(8) - (mInnerBottomTextHeight - caculateTextCenterHeightDisTance(mInnerPaint)), mInnerPaint);
        mInnerPaint.setAlpha(255);
    }

    /**
     * 画中心内容
     *
     * @param canvas
     */
    private void drawInnerText(Canvas canvas) {
        switch (mCurrentMode) {
            case MODE_TYPE_ON://on或者Off模式时，画中心的内容
            case MODE_TYPE_OFF://on或者Off模式时，画中心的内容
                //画中心顶部的温度
                drawInnterTextByOnOrOff(canvas, innerCircleTmp, innerCircleTmpUnit, innerCircleTime, innerCircleSpeed);
                break;
            case MODE_TYPE_AUTO_TMP_C:
            case MODE_TYPE_AUTO_TMP_F:
            case MODE_TYPE_AUTO_HUM:
                //画顶部时间文字
                drawInnerTextByAuto(canvas, innerCircleTime, innerCircleTmp, innerCircleTmpUnit, innerCircleSpeed);
                break;
            case MODE_TYPE_TIME_TO_ON:
            case MODE_TYPE_TIME_TO_OFF:
            case MODE_TYPE_CYCLE:
                drawInnerTextByTimeToOn(canvas, statusCenterText, innerCircleTmp, innerCircleTmpUnit, innerCircleSpeed, statusCenterBottomText);
                break;
            case MODE_TYPE_SCHEDULE:
                drawInnerTextByTimeToOn(canvas, innerCircleTime, innerCircleTmp, innerCircleTmpUnit, innerCircleSpeed, statusCenterBottomText);
                break;
            default:
        }
    }

    /**
     * 设置内部圆的数据
     *
     * @param circleTmp
     * @param circleTmpUnit
     * @param cycleTime
     * @param CycleSpeed
     */
    public void setInnerCircleText(String circleTmp, String circleTmpUnit, String cycleTime, String CycleSpeed, byte status) {
        this.innerCircleTmp = circleTmp;
        this.innerCircleTmpUnit = circleTmpUnit;
        this.innerCircleSpeed = CycleSpeed;
        this.innerCircleTime = cycleTime;
        this.innerState = status;
    }

    public void setTmp(String circleTmp, String circleTmpUnit) {
        this.innerCircleTmp = circleTmp;
        this.innerCircleTmpUnit = circleTmpUnit;
    }

    private void initBackground() {
        Paint paint = new Paint(Paint.DITHER_FLAG | Paint.ANTI_ALIAS_FLAG);
        paint.setStrokeWidth(ConvertUtils.dp2px(1));
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.WHITE);
        int radius = outCycleRadius;
        BlurMaskFilter filter = new BlurMaskFilter(maxR - radius - ConvertUtils.dp2px(2), BlurMaskFilter.Blur.OUTER);
        //阴影 15f 阴影长度
        paint.setMaskFilter(filter);

        background = Bitmap.createBitmap(mWidth, mWidth, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(background);
        canvas.drawCircle(maxR, maxR, radius, paint);

        paint.setMaskFilter(null);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(mMiddRStrok);
        LinearGradient mLinearGradient = new LinearGradient(mMiddRStrok, 0, mWidth - mMiddRStrok, 0,
                new int[]{getResources().getColor(R.color.color_21409A), getResources().getColor(R.color.color_00B8EC)}, null, Shader.TileMode.CLAMP);
        paint.setShader(mLinearGradient);
        int stroke = mMiddRStrok / 2;
//        半径为布局宽度一半 + 最外圆内边距 + 中圆的线宽度的一半
        radius = mInnerCycleWithMidCycleMargin + mInnerR + stroke;
        mMiddRadias = radius;
        canvas.drawCircle(maxR, maxR, radius, paint);
//        画中圆的外阴影
        paint.setStrokeWidth(ConvertUtils.dp2px(2));
        paint.setColor(getResources().getColor(R.color.color_80000000));
        paint.setShader(null);
        filter = new BlurMaskFilter(20f, BlurMaskFilter.Blur.OUTER);
        paint.setMaskFilter(filter);
        canvas.drawCircle(maxR, maxR, radius + stroke + ConvertUtils.dp2px(1), paint);
        //画中圆的内阴影
        paint.setColor(getResources().getColor(R.color.color_80FFFFFF));
        paint.setStrokeWidth(ConvertUtils.dp2px(1));
        paint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(maxR, maxR, radius - stroke, paint);

        paint.setMaskFilter(null);
        paint.setColor(getResources().getColor(R.color.color_292929));
        paint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(maxR, maxR, mInnerR, paint);
    }

    /**
     * 初始化内圆画笔
     */
    private void initInnerCyclePaint() {
        mInnerPaint = new Paint();
        mInnerPaint.setAntiAlias(true);
        mInnerPaint.setStyle(Paint.Style.FILL);
    }

    /**
     * 计算文字根据baseline中心点距离
     *
     * @param paint
     * @return
     */
    private float caculateTextCenterHeightDistancByBaseLine(Paint paint) {
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        return (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
    }

    /**
     * 计算文字的最小TOP和最小Bottom
     * 计算文字高度
     *
     * @param paint 实时传入的paint 大小有影响
     * @return 返回文字的最小高度的一半 用作居中
     */
    private int caculateTextCenterHeightDisTance(Paint paint) {
        Paint.FontMetrics fm = paint.getFontMetrics();
        Rect minRect = new Rect();
        paint.getTextBounds("0", 0, "0".length(), minRect);
        //最小矩形，实际top位置
        int minTop = minRect.top + maxR;
        //最小矩形，实际bottom位置
        int minBottom = minRect.bottom + maxR;
        return (minBottom - minTop);
    }


    /**
     * 初始化开始和结束的圆圈view
     *
     * @param currentStartTag  开始圆圈的目标值
     * @param currentStartText 开始圆圈的目标显示值
     * @param currentEndTag    开始圆圈的目标值
     * @param currentEndText   开始圆圈的目标显示值
     */
    public void initStartOrEndAngle(int currentStartTag, int currentStartText, int currentEndTag, int currentEndText) {
        this.currentStartTag = currentStartTag;
        this.currentEndTag = currentEndTag;
        switch (mCurrentMode) {
            case MODE_TYPE_CYCLE:
                tempTextAngle = (float) 142 / realMaxTotal;//计算读数最小角度
                break;
            default:
                tempTextAngle = (float) 300 / realMaxTotal;//计算读数最小角度
                break;
        }
        this.needRotatteSartAngel = tempTextAngle * currentStartTag;

        this.needRotatteEndAngel = tempTextAngle * currentEndTag;
        currentStartShowText = currentStartText;
        currentEndShowText = currentEndText;
        postInvalidate();
    }


    /**
     * 开始滑动
     *
     * @param event
     */
    private void startDragging(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        if (showFan != 0 || (mCurrentMode != MODE_TYPE_ON && mCurrentMode != MODE_TYPE_OFF)) {
            isStartCanDraging = isInCircle(needRotatteSartAngel, mMiddRadias, maxR, maxR, x, y);
        }
        if (!isStartCanDraging) {
            if (mCurrentMode == MODE_TYPE_CYCLE) {
                isEndCanDraging = endTagViewIsCanMove(needRotatteEndAngel + 158, mMiddRadias, maxR, maxR, x, y);
            } else {
                isEndCanDraging = endTagViewIsCanMove(needRotatteEndAngel, outCycleRadius, maxR, maxR, x, y);
            }
        }
        isStartDraging = true;
        isMoved = true;
        if ((isStartCanDraging || isEndCanDraging) && mOnChangeListeners != null) {
            mOnChangeListeners.onChangeTouchStart(isStartCanDraging);
        }
    }


    /**
     * 滑动中
     *
     * @param event
     */
    private boolean onDragging(MotionEvent event) {
        if (!isStartCanDraging && !isEndCanDraging || !isMoved) {
            return false;
        }
        float x = event.getX();
        float y = event.getY();
        float centerX = getWidth() / 2;
        float centerY = getHeight() / 2;
        //得到当前优化后的角度
        int rotationBetweenLines = getRotationBetweenLinesForMark(centerX, centerY, x, y);
        //循环模式
        if (mCurrentMode == MODE_TYPE_CYCLE) {
            if (isStartCanDraging) {
                if (rotationBetweenLines < 0) {
                    if (rotationBetweenLines < -10) {//估值 小于-10度角时就不让滑动了
                        isMoved = false;
                    }
                    rotationBetweenLines = 0;
                }
                if (rotationBetweenLines > 142) {
                    if (rotationBetweenLines > 152) {
                        isMoved = false;
                    }
                    rotationBetweenLines = 142;
                }
                currentStartTag = (int) (rotationBetweenLines / tempAngle);
                currentStartShowText = (int) (rotationBetweenLines / tempTextAngle / 15) * 15;
                needRotatteSartAngel = rotationBetweenLines;
                needRoatteStatusAngel = rotationBetweenLines;
                if (null != mOnChangeListeners) {
                    mOnChangeListeners.onChangeStartAndEnd(currentStartShowText, currentEndShowText);
                }
                postInvalidate();
                return true;
            }
            if (isEndCanDraging) {
                if (rotationBetweenLines < 158) {
                    if (rotationBetweenLines < 148) {//估值 小于-10度角时就不让滑动了
                        isMoved = false;//防止点击不滑动直接抬起造成的移动
                    }
                    rotationBetweenLines = 158;
                }
                if (rotationBetweenLines > 300) {
                    if (rotationBetweenLines > 310) {
                        isMoved = false;//防止点击不滑动直接抬起造成的移动
                    }
                    rotationBetweenLines = 300;
                }
                currentEndTag = (int) ((rotationBetweenLines - 158) / tempAngle);
                currentEndShowText = (int) ((rotationBetweenLines - 158) / tempTextAngle / 15) * 15;
                needRotatteEndAngel = rotationBetweenLines - 158;
                needRoatteStatusAngel = rotationBetweenLines;
                if (null != mOnChangeListeners) {
                    mOnChangeListeners.onChangeStartAndEnd(currentStartShowText, currentEndShowText);
                }
                postInvalidate();
                return true;
            }
        } else {
            if (rotationBetweenLines < 0) {
                if (isStartCanDraging) {
                    //因为涉及到点击区域是圆型，小于起始点并且在园内也可以点击
                    if (rotationBetweenLines < -10) {
                        //估值 小于-10度角时就不让滑动了
                        isMoved = false;
                    }
                    rotationBetweenLines = 0;
                    currentStartTag = (int) (rotationBetweenLines / tempAngle);
                    currentStartShowText = (int) (rotationBetweenLines / tempTextAngle);
                }
                if (isEndCanDraging) {
                    if (rotationBetweenLines < -10) {
                        //估值 小于-10度角时就不让滑动了
                        isMoved = false;
                    }
                    rotationBetweenLines = 0;
                    currentEndTag = (int) (rotationBetweenLines / tempAngle);
                    currentEndShowText = (int) (rotationBetweenLines / tempTextAngle);
                }
                if (null != mOnChangeListeners) {
                    mOnChangeListeners.onChangeStartAndEnd(currentStartShowText, currentEndShowText);
                }
                postInvalidate();
                return true;
            }
            if (rotationBetweenLines > 300) {
                if (isStartCanDraging) {
                    if (rotationBetweenLines > 310) {
                        isMoved = false;
                    }
                    rotationBetweenLines = 300;
                    currentStartTag = (int) (rotationBetweenLines / tempAngle);
                    currentStartShowText = (int) (rotationBetweenLines / tempTextAngle);
                    needRotatteSartAngel = rotationBetweenLines;
                }
                if (isEndCanDraging) {
                    if (rotationBetweenLines > 310) {
                        isMoved = false;
                    }
                    rotationBetweenLines = 300;
                    currentEndTag = (int) (rotationBetweenLines / tempAngle);
                    currentEndShowText = (int) (rotationBetweenLines / tempTextAngle);
                    needRotatteEndAngel = rotationBetweenLines;
                }
                if (null != mOnChangeListeners) {
                    mOnChangeListeners.onChangeStartAndEnd(currentStartShowText, currentEndShowText);
                }
                postInvalidate();
                return true;
            }
        }
        //开始圆球是否可以滑动
        if (isStartCanDraging) {
            needRotatteSartAngel = rotationBetweenLines;
            //开始圆球
            mStartTagViewRealRotateAngel = rotationBetweenLines;
            currentStartTag = (int) (rotationBetweenLines / tempAngle);
            if (mCurrentMode == MODE_TYPE_TIME_TO_OFF || mCurrentMode == MODE_TYPE_TIME_TO_ON || mCurrentMode == MODE_TYPE_SCHEDULE) {
                currentStartShowText = (int) (mStartTagViewRealRotateAngel / tempTextAngle / 15) * 15;
            } else {
                currentStartShowText = (int) (mStartTagViewRealRotateAngel / tempTextAngle);
            }
        }
        //结束圆球是否可以滑动
        if (isEndCanDraging) {
            needRotatteEndAngel = rotationBetweenLines;//结束圆球
            mEndTagViewRealRotateAngel = rotationBetweenLines;//结束圆球实际角度
            currentEndTag = (int) (needRotatteEndAngel / tempAngle);
            if (mCurrentMode == MODE_TYPE_SCHEDULE) {
                currentEndShowText = (int) (needRotatteEndAngel / tempTextAngle / 15) * 15;
            } else {
                currentEndShowText = (int) (needRotatteEndAngel / tempTextAngle);
            }
        }
        if (null != mOnChangeListeners) {
            mOnChangeListeners.onChangeStartAndEnd(currentStartShowText, currentEndShowText);
        }
        postInvalidate();
        return true;
    }


    /**
     * 结束滑动
     *
     * @param event
     */
    private void stopDragging(MotionEvent event) {
//        if (!isMoved) return;//没有滑动 不响应事件
        //抬起 重置可滑动状态
        if (!isMoved) {
            isStartCanDraging = false;
            isEndCanDraging = false;
            isStartDraging = false;
            if (null != mOnChangeListeners) {
                mOnChangeListeners.onChangeAndEndByTouchEnd(currentStartShowText, currentEndShowText);
            }
            return;
        }
        if (!isStartCanDraging && !isEndCanDraging) return;
        float x = event.getX();
        float y = event.getY();
        float centerX = maxR;
        float centerY = maxR;
        float rotationBetweenLines = getRotationBetweenLinesForMark(centerX, centerY, x, y);
        float leftRotation = rotationBetweenLines % tempAngle;
        if (mCurrentMode == MODE_TYPE_CYCLE && isEndCanDraging) {
            leftRotation = (rotationBetweenLines - 158) % tempAngle;
        }
        //计算剩余角度是否超过最小间隔刻度的一半 超过则跳转到下一个刻度
        if (mCurrentMode == MODE_TYPE_ON || mCurrentMode == MODE_TYPE_OFF) {
            if (leftRotation > tempAngle / 2) {
                if (isStartCanDraging) {
                    currentStartTag++;
                }
                if (isEndCanDraging) {
                    currentEndTag++;
                }
            }
            if (maxTotal == realMaxTotal) {
                currentStartShowText = currentStartTag;
                currentEndShowText = currentEndTag;
            }
            if (null != mOnChangeListeners) {
                mOnChangeListeners.onChangeStartAndEnd(currentStartShowText, currentEndShowText);
            }
        }
        postInvalidate();
        isStartCanDraging = false;
        isEndCanDraging = false;
        isStartDraging = false;
        isMoved = false;
        if (null != mOnChangeListeners) {
            mOnChangeListeners.onChangeAndEndByTouchEnd(currentStartShowText, currentEndShowText);
        }
    }

    /**
     * 判断是否在圆内
     *
     * @param angle          当前角度
     * @param caculateRadius 计算圆上坐标时的半径
     * @param circleRadius   计算是否在圆内的判断半径
     * @param centerX        圆心X坐标
     * @param centerY        圆心Y坐标
     * @param touchX         点击时的X坐标
     * @param touchY         点击时的Y坐标
     * @return
     */
    private boolean isInCircle(float angle, int caculateRadius, int circleRadius, int centerX, int centerY, int touchX, int touchY) {
        //计算当前角度圆上的坐标
        if (angle > 240) {
            angle = angle - 240;
        } else {
            angle = angle + 120;
        }
        double[] doubles = caculateXY(centerX, centerY, caculateRadius, angle);
        //点击位置x坐标与圆心的x坐标的距离
        int distanceX = Math.abs((int) (doubles[0] - touchX));
        //点击位置y坐标与圆心的y坐标的距离
        int distanceY = Math.abs((int) (doubles[1] - touchY));
        //点击位置与圆心的直线距离
        int distanceZ = (int) (Math.pow(distanceX, 2) + Math.pow(distanceY, 2));
        //如果点击位置与圆心的距离大于圆的半径，证明点击位置没有在圆内
        if (distanceZ > Math.pow(circleRadius, 2)) {
            return false;
        }
        return true;
    }

    private boolean isInCircle(float angle, int radius, int centerX, int centerY, float touchX, float touchY) {
        angle += 30;
        int cycleRadius = ConvertUtils.dp2px(slidingBlockRadias);
        RectF rectF = new RectF(centerX - cycleRadius, centerY + mInnerR + mInnerCycleWithMidCycleMargin,
                centerX + cycleRadius, centerY + radius + 2 * cycleRadius);
        Matrix matrix = new Matrix();
        matrix.setRotate(angle, centerX, centerY);
        matrix.mapRect(rectF);
        return rectF.contains(touchX, touchY);
    }

    /**
     * 计算圆上的坐标
     *
     * @param centerX 圆心x坐标
     * @param centerY 圆心Y坐标
     * @param radius  半径
     * @param angle   角度
     */
    private double[] caculateXY(int centerX, int centerY, int radius, float angle) {
        double[] ints = new double[2];
        double pointX = centerX + radius * Math.cos(angle * PI / 180);
        double pointY = centerY + radius * Math.sin(angle * PI / 180);
        ints[0] = pointX;
        ints[1] = pointY;
        return ints;
    }


    /**
     * 是否可以滑动结束圆
     *
     * @param angle
     * @param caculateRadius
     * @param circleRadius   手指触摸滑块的有效点击半径
     * @param centerX
     * @param centerY
     * @param touchX
     * @param touchY
     * @return
     */
    private boolean endTagViewIsCanMove(float angle, int caculateRadius, int circleRadius, int centerX, int centerY, int touchX, int touchY) {
        boolean inCircle = isInCircle(angle, caculateRadius, circleRadius, centerX, centerY, touchX, touchY);
        return inCircle && (mCurrentMode == MODE_TYPE_CYCLE || mCurrentMode == MODE_TYPE_SCHEDULE
                || mCurrentMode == MODE_TYPE_AUTO_TMP_C || mCurrentMode == MODE_TYPE_AUTO_TMP_F ||
                mCurrentMode == MODE_TYPE_AUTO_HUM);
    }

    private boolean endTagViewIsCanMove(float angle, int radius, int centerX, int centerY, float touchX, float touchY) {
        boolean inCircle = isInCircle(angle, radius, centerX, centerY, touchX, touchY);
        return inCircle && (mCurrentMode == MODE_TYPE_CYCLE || mCurrentMode == MODE_TYPE_SCHEDULE
                || mCurrentMode == MODE_TYPE_AUTO_TMP_C || mCurrentMode == MODE_TYPE_AUTO_TMP_F ||
                mCurrentMode == MODE_TYPE_AUTO_HUM);
    }

    /**
     * 针对当前ui优化的角度计算
     * 获得当前旋转角度
     *
     * @param centerX
     * @param centerY
     * @param xInView
     * @param yInView
     * @return
     */
    private int getRotationBetweenLinesForMark(float centerX, float centerY, float xInView, float yInView) {
        double rotation = 0;
        double k1 = (double) (centerY - centerY) / (centerX * 2 - centerX);
        double k2 = (double) (yInView - centerY) / (xInView - centerX);
        double tmpDegree = Math.atan((Math.abs(k1 - k2)) / (1 + k1 * k2)) / PI * 180;
        if (xInView > centerX && yInView < centerY) {  //第一象限
            rotation = 240 - tmpDegree;
        } else if (xInView > centerX && yInView > centerY) //第二象限
        {
            rotation = 240 + tmpDegree;
        } else if (xInView < centerX && yInView > centerY) { //第三象限
            rotation = 60 - tmpDegree;
        } else if (xInView < centerX && yInView < centerY) { //第四象限
            rotation = 60 + tmpDegree;
        } else if (xInView == centerX && yInView < centerY) {
            rotation = 0;
        } else if (xInView == centerX && yInView > centerY) {
            rotation = 180;
        }

        return (int) rotation;
    }

    /**
     * Cycle模式End滑点文字显示
     *
     * @param canvas
     * @param content
     */
    private void drawTextByCyclePointByCycleEnd(Canvas canvas, String content, float endAngel) {
        if (endAngel < 38) {
            canvas.translate(0, -(mMiddRadias + ConvertUtils.dp2px(mMiddRStrok) / 2 + ConvertUtils.dp2px(18)));//坐标移置左侧
            canvas.rotate(-(endAngel + 8), 0, 0);
            mArcTextPaint.setTextAlign(Paint.Align.LEFT);
            mArcTextPaint.setColor(getResources().getColor(R.color.color_15BAFF));
            canvas.drawText(content, 0, caculateTextCenterHeightDistancByBaseLine(mArcTextPaint), mArcTextPaint);
        } else if (endAngel > 37 && endAngel < 112) {
            canvas.translate(-(ConvertUtils.dp2px(13) + caculateTextCenterHeightDisTance(mArcTextPaint)), -(mMiddRadias + ConvertUtils.dp2px(mMiddRStrok) / 2));//坐标移置左侧
            canvas.rotate(-(endAngel + 8), 0, 0);
            mArcTextPaint.setTextAlign(Paint.Align.LEFT);
            mArcTextPaint.setColor(getResources().getColor(R.color.color_15BAFF));
            canvas.drawText(content, 0, caculateTextCenterHeightDistancByBaseLine(mArcTextPaint), mArcTextPaint);
        } else {
            canvas.translate(0, -(mMiddRadias + ConvertUtils.dp2px(mMiddRStrok) / 2 + ConvertUtils.dp2px(18)));//坐标移置左侧
            canvas.rotate(-(endAngel + 8), 0, 0);
            mArcTextPaint.setTextAlign(Paint.Align.LEFT);
            mArcTextPaint.setColor(getResources().getColor(R.color.color_15BAFF));
            canvas.drawText(content, 0, caculateTextCenterHeightDistancByBaseLine(mArcTextPaint), mArcTextPaint);
        }
    }

    /**
     * 根据不同模式显示当前的状态线和中心on或者off文字
     *
     * @param time    时间  单位秒
     * @param onOrOff
     */
    public void setStatusLineAndCenterText(String time, String onOrOff) {
        switch (mCurrentMode) {
            case MODE_TYPE_ON:
                break;
            case MODE_TYPE_OFF:
                break;
            case MODE_TYPE_CYCLE:
                boolean isOff = "0".equals(onOrOff);
                if (!TextUtils.isEmpty(time)) {
                    long cycleTime = Long.parseLong(time);
                    needRoatteStatusAngel = cycleTime * tempTextAngle / 60 + (isOff ? 0 : 158);
                    int timeToOnHour = (int) (cycleTime / 3600);
                    int timeToOnMints = (int) (cycleTime % 3600) / 60;
                    int timeSecond = (int) ((cycleTime % 3600) % 60);
                    if (timeSecond > 0) {
                        if (timeToOnMints >= 59) {
                            timeToOnHour++;
                            timeToOnMints = 0;
                        } else {
                            if (timeToOnHour == 0 && timeToOnMints == 0) {
                            } else {
                                timeToOnMints++;
                            }
                        }
                    }
                    if (timeToOnHour == 0 && timeToOnMints == 0 && timeSecond != 0) {
                        statusCenterText = String.format(Locale.ENGLISH, "%d", timeSecond);
                    } else {
                        statusCenterText = String.format(Locale.ENGLISH, "%d:%02d", timeToOnHour, timeToOnMints);
                    }
                    statusCenterBottomText = isOff ? "TO OFF" : "TO ON";
                }
                break;
            case MODE_TYPE_AUTO_TMP_C:
            case MODE_TYPE_AUTO_TMP_F:
            case MODE_TYPE_AUTO_HUM:
                if (!TextUtils.isEmpty(time)) {
                    needRoatteStatusAngel = Integer.valueOf(time) * tempTextAngle;
                }
                break;
            case MODE_TYPE_SCHEDULE:
                if (!TextUtils.isEmpty(time)) {
                    long schedTime = Long.parseLong(time);
                    needRoatteStatusAngel = (schedTime / 60) * tempTextAngle;
                    statusCenterBottomText = onOrOff;
                }
                break;
            case MODE_TYPE_TIME_TO_ON:
            case MODE_TYPE_TIME_TO_OFF:
                if (!TextUtils.isEmpty(time)) {
                    long timeToOn = Long.parseLong(time);
                    needRoatteStatusAngel = timeToOn * tempTextAngle / 60;
                    int timeToOnHour = (int) (timeToOn / 3600);
                    int timeToOnMints = (int) ((timeToOn % 3600) / 60);
                    int timeSecond = (int) ((timeToOn % 3600) % 60);
                    if (timeSecond > 0) {
                        if (timeToOnMints >= 59) {
                            timeToOnHour++;
                            timeToOnMints = 0;
                        } else {
                            if (timeToOnHour == 0 && timeToOnMints == 0) {
                            } else {
                                timeToOnMints++;
                            }
                        }
                    }
                    if (timeToOnHour == 0 && timeToOnMints == 0 && timeSecond != 0) {
                        statusCenterText = String.format(Locale.ENGLISH, "%d", timeSecond);
                    } else {
                        statusCenterText = String.format(Locale.ENGLISH, "%d:%02d", timeToOnHour, timeToOnMints);
                    }
                    statusCenterBottomText = "0".equals(onOrOff) ? "TO OFF" : "TO ON";
                }
                break;
        }
        postInvalidate();
    }

    /**
     * 设置监听器
     */
    public void setOnChangeListeners(OnChangeListeners onChangeListeners) {
        mOnChangeListeners = onChangeListeners;
    }

    /**
     * 初始化监听器
     */
    public interface OnChangeListeners {
        void onChangeTouchStart(boolean isStart);

        void onChangeStartAndEnd(int start, int end);

        //结束滑动时的开始和结束点
        void onChangeAndEndByTouchEnd(int start, int end);
    }


    private void initAnimator() {
        mProgressAnim = ObjectAnimator.ofInt(this, "alphaa", 255, 0, 255);
        mProgressAnim.setDuration(1000);
        mProgressAnim.setInterpolator(null);
    }

    //开始动画
    public void startAnimation() {
        mProgressAnim.start();
    }

    public int getAlphaa() {
        return mAlpha;
    }

    public void setAlphaa(int alphaa) {
        mAlpha = alphaa;
        invalidate();
    }
}
